/**
 * BottomNavigationBar、PageView结合Provider切换页面
 */

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import './airplay_page.dart';
import './emial_page.dart';
import './home_page.dart';
import './pages_page.dart';
import '../provider/current_index_provider.dart';

class BottomNavigationBarPage extends StatelessWidget {
  final List<BottomNavigationBarItem> items = [
    BottomNavigationBarItem(
        backgroundColor: Colors.lightBlue,
        title: Text("Home"),
        icon: Icon(Icons.home, color: Colors.white)),
    BottomNavigationBarItem(
        backgroundColor: Colors.lightBlue,
        title: Text("Email"),
        icon: Icon(Icons.email, color: Colors.white)),
    BottomNavigationBarItem(
        backgroundColor: Colors.lightBlue,
        title: Text("Pages"),
        icon: Icon(Icons.pages, color: Colors.white)),
    BottomNavigationBarItem(
        backgroundColor: Colors.lightBlue,
        title: Text("Ariplay"),
        icon: Icon(Icons.airplay, color: Colors.white))
  ];
  final List<Widget> pages = [
    HomePage(),
    EmailPage(),
    PagesPage(),
    AirplayPage()
  ];

  var _controller = PageController(initialPage: 0);

  @override
  Widget build(BuildContext context) {
    return Provider<int>.value(
        value: 0,
        child: ChangeNotifierProvider.value(
          value: CurrentIndexProvider(),
          child: Consumer<CurrentIndexProvider>(
              builder: (context, CurrentIndexProvider provider, child) {
            return Scaffold(
              appBar: AppBar(
                title: Text("BottomNavigationBarPage"),
                backgroundColor: Colors.lightBlue,
              ),
              bottomNavigationBar: BottomNavigationBar(
                items: items,
                currentIndex: provider.currentIndex,
                onTap: (index) {
                  _controller.jumpToPage(index);
                  provider.updateCurrentIndex(index);
                },
              ),
              body: PageView(
                controller: _controller,
                children: pages,
                onPageChanged: (index) {
                  provider.updateCurrentIndex(index);
                },
              ),
            );
          }),
        ));
  }
}
